
export default {
    props: {
        tableData: Array,
        searchCondition: Object,
        apiRequestCallback: {
            type: Function,
            default: null
        },
        index: {
            type: Boolean,
            default: false
        },
        loading: {
            type: Boolean,
            default: false
        },
        isSmall: {
            type: Boolean,
            default: false
        }

    },
    data() {
        return {
            current: 1,
            pageSizeOpts: ['5', '10', '50', '100'],
            pagingModel: {
                current: 1,
                total: 1,
                size: 10
            }
        }
    },
    computed: {
        searchBtn() {
            return this.searchCondition.searchClk
        }
    },
    watch: {
        searchBtn: {
            handler(newVal, oldVal) {
                if (oldVal < newVal) {
                    this.getFirstPage()
                } else {
                    this.getCurrentPage()
                }

            }
        }
    },
    mounted() {
       
    },
    created(){
    },
    methods: {
        getCurrentPage() {
            let params;
            params = this.deepCopy(this.searchCondition);
            delete params.searchClk;
            // 去除空格
            for (let k in params) {
                params[k] && typeof (params[k]) == 'String' ? params[k] = params[k].trim() : params[k]
            }
            if (this.pagingModel.total < this.pagingModel.current && this.pagingModel.total !== 0) {
                this.pagingModel.current = this.pagingModel.total
            }
            params.current = this.pagingModel.current;
            params.size = this.pagingModel.size;
            this.$emit('update:loading', true)
            if (this.apiRequestCallback != null) {
                this.apiRequestCallback(params).then(data => {
                    this.pagingModel.size = data.data.data.size;
                    this.pagingModel.total = data.data.data.total;
                    this.pagingModel.current = data.data.data.current;
                    this.$emit('update:tableData', data.data.data.records)
                    this.$emit('update:loading', false)
                    if ((data.data.data.current > data.data.data.pages) && data.data.data.pages !== 0) {
                        this.pagingModel.current = data.data.data.pages
                        this.getCurrentPage()
                        return
                    }
                }).catch(() => {
                    this.$emit('update:loading', false);
                })
              
            }
        },
        deepCopy(data){
            return JSON.parse(JSON.stringify(data))
        },
        onCurrentChange(current) {
            this.pagingModel.current = current;
            this.getCurrentPage();
        },
        onSizeChange(current, size) {
            this.pagingModel.size = size;
            this.pagingModel.current = 1;
            this.getCurrentPage();
        },
        getFirstPage() {
            this.current = 1;
            this.pagingModel.current = 1;
            this.getCurrentPage();
        },
    },
}